package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.query.seller.trading.api.parameter.FlowsDetail;
import com.ymatou.query.seller.trading.api.parameter.FundSummary;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFlowsResult;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFundDetailBean;
import com.ymatou.query.seller.trading.api.parameter.GetOrderFundDetailResponse;
import com.ymatou.query.seller.trading.api.parameter.Sp_GetOrderFundDetail_v1;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.PaymentProxyCallService;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * @date 2016-06-28
 * @author songyefeng
 * @desc 获取订单资金明细
 */
@TestSuite
public class Ts_GetOrderFundDetail {

	private static GetOrderFundDetailBean request;
	//	private static com.ymatou.query.seller.trading.api.service.GetOrderFundDetailCall call;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetOrderFundDetailCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单资金明细");
	}

	@Before
	public void caseUp() {
		request = new GetOrderFundDetailBean();
		//		call = new com.ymatou.query.seller.trading.api.service.GetOrderFundDetailCall();
		call = new com.ymatou.iapi.trading.sellerquery2.service.GetOrderFundDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetOrderFundDetailResponse expectedResponse = new GetOrderFundDetailResponse();
		ConstructExpectedResponse(expectedResponse);
		GetOrderFundDetailResponse actualResponse = call
				.getOrderFundDetailResponse();
		// DataUtil.verifyEquals(expectedResponse.isOnlyNeedPayOnce(),
		// actualResponse.isOnlyNeedPayOnce(), "验证OnlyNeedPayOnce");
		DataUtil.verifyEquals(true, actualResponse.isOnlyNeedPayOnce(),
				"验证OnlyNeedPayOnce");
		Logger.comment("************************开始验证Flows节点************************");
		List<FlowsDetail> expFlows = expectedResponse.getFlows();
		List<FlowsDetail> actFlows = actualResponse.getFlows();
		if (DataUtil.isNullOrEmpty(expFlows)) {
			DataUtil.verify(true, DataUtil.isNullOrEmpty(actFlows),
					"验证Flows == null 或者为 空列表");
		} else {
			DataUtil.verifyEquals(expFlows.size(), actFlows.size(),
					"验证Flows.size()");
			for (FlowsDetail exp : expFlows) {
				boolean exists = actFlows.stream().anyMatch(x -> x.equals(exp));
				DataUtil.verifyEquals(true, exists, "验证值为：\n" + exp.toString()
						+ "\n的Flow对象是否在Flows中");
			}
		}
		Logger.comment("************************完成开始验证Flows节点************************");

		Logger.comment("************************开始验证Summary节点************************");
		FundSummary expSum = expectedResponse.getSummary();
		FundSummary actSum = actualResponse.getSummary();
		DataUtil.verifyEquals(expSum == null, actSum == null, "验证Summary节点是否为空");
		if (actSum != null) {
			// 第三方优惠总金额
			DataUtil.verify(expSum.getTotalThirdPartyDiscount(),
					actSum.getTotalThirdPartyDiscount(),
					"验证Summary节点的TotalThirdPartyDiscount");
			DataUtil.verify(expSum.getPaidCash(), actSum.getPaidCash(),
					"验证Summary节点的PaidCash");
			DataUtil.verify(expSum.getPaidGift(), actSum.getPaidGift(),
					"验证Summary节点的PaidGift");
			DataUtil.verify(expSum.getPaidFreeCard(), actSum.getPaidFreeCard(),
					"验证Summary节点的PaidFreeCard");
			DataUtil.verify(expSum.getPaidYmtCoupon(),
					actSum.getPaidYmtCoupon(), "验证Summary节点的PaidYmtCoupon");
			DataUtil.verify(expSum.getPaidSellerCoupon(),
					actSum.getPaidSellerCoupon(),
					"验证Summary节点的PaidSellerCoupon");
			DataUtil.verify(expSum.getPostPaidCash(), actSum.getPostPaidCash(),
					"验证Summary节点的PostPaidCash");
			DataUtil.verify(expSum.getPostPaidGift(), actSum.getPostPaidGift(),
					"验证Summary节点的PostPaidGift");
			DataUtil.verify(expSum.getTotalProductPrice(),
					actSum.getTotalProductPrice(),
					"验证Summary节点的TotalProductPrice");
			DataUtil.verify(expSum.getFirstPayProductPrice(),
					actSum.getFirstPayProductPrice(),
					"验证Summary节点的FirstPayProductPrice");
			DataUtil.verify(expSum.getFreight(), actSum.getFreight(),
					"验证Summary节点的Freight");
			DataUtil.verify(expSum.getOrderDiscount(),
					actSum.getOrderDiscount(), "验证Summary节点的OrderDiscount");
			DataUtil.verify(expSum.getPostPayDiscount(),
					actSum.getPostPayDiscount(), "验证Summary节点的PostPayDiscount");
			DataUtil.verify(DataUtil.BigDecimall.GetValueOrDefault(expSum
					.getTotalRefundedCash()), DataUtil.BigDecimall
					.GetValueOrDefault(actSum.getTotalRefundedCash()),
					"验证Summary节点的TotalRefundedCash");
			DataUtil.verify(DataUtil.BigDecimall.GetValueOrDefault(expSum
					.getTotalRefundedGift()), DataUtil.BigDecimall
					.GetValueOrDefault(actSum.getTotalRefundedGift()),
					"验证Summary节点的TotalRefundedGift");
			DataUtil.verify(DataUtil.BigDecimall.GetValueOrDefault(expSum
					.getTotalRefundedYmtCoupon()), DataUtil.BigDecimall
					.GetValueOrDefault(actSum.getTotalRefundedYmtCoupon()),
					"验证Summary节点的TotalRefundedYmtCoupon");
			DataUtil.verify(DataUtil.BigDecimall.GetValueOrDefault(expSum
					.getTotalActivityPrice()), DataUtil.BigDecimall
					.GetValueOrDefault(actSum.getTotalActivityPrice()),
					"验证Summary节点的TotalActivityPrice");
			DataUtil.verify(DataUtil.BigDecimall.GetValueOrDefault(expSum
					.getTotalCanceledCash()), DataUtil.BigDecimall
					.GetValueOrDefault(actSum.getTotalCanceledCash()),
					"验证Summary节点的TotalCanceledCash");
			DataUtil.verify(DataUtil.BigDecimall.GetValueOrDefault(expSum
					.getTotalCanceledYmtCoupon()), DataUtil.BigDecimall
					.GetValueOrDefault(actSum.getTotalCanceledYmtCoupon()),
					"验证Summary节点的TotalCanceledYmtCoupon");
		}
		Logger.comment("************************完成开始验证Summary节点************************");
	}

	private void ConstructExpectedResponse(GetOrderFundDetailResponse response) {
		Sp_GetOrderFundDetail_v1 sp = new Sp_GetOrderFundDetail_v1();
		sp.setOrderId(DataUtil.Integerr.GetValueOrDefault(request.getOrderId()));
		sp.setSummary(request.getSummary());
		sp.execute();
		GetOrderFlowsResult result = Convert(sp);
		response.setOnlyNeedPayOnce(!sp.isNeedPayTwiceOrMore());
		response.setFlows(result.getFlows());
		response.setSummary(result.getSummary());
	}

	private GetOrderFlowsResult Convert(Sp_GetOrderFundDetail_v1 sp) {
		GetOrderFlowsResult o = new GetOrderFlowsResult();
		List<FlowsDetail> flows = sp.getFundDetails();
		if (!DataUtil.isNullOrEmpty(flows)) {
			// List<FlowsDetail> flows2 = sp.getFundDetails();
			flows.stream().forEach(x -> {
				if (x.getOperation() != null && x.getOperation() == 2) {
					x.setThirdPartyPaidAmount(x.getCash());
					x.setAccountPaidAmount(BigDecimal.ZERO);
				}
			});
			o.setFlows(flows);
		}

		o.setSummary(sp.getSummary());
		return o;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_001() {
		Logger.start(true, "获取订单资金明细, summary=true, iTradingStatus=4");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create4_ReceivedOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");

			request.setOrderId(orderId);
			request.setSummary(true);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_002() {
		Logger.start(true, "获取订单资金明细, summary=false");
		try {
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			request.setOrderId(orderId);
			request.setSummary(false);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_003() {
		Logger.start(true, "获取订单资金明细, summary=false, iTradingStatus=1");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create1_M2cOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.setData(request);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_004() {
		Logger.start(true, "获取订单资金明细, summary=true, iTradingStatus=2");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create2_PayOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(true);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_005() {
		Logger.start(true, "获取订单资金明细, summary=false, iTradingStatus=3");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create3_ShippedOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(false);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_006() {
		Logger.start(true, "获取订单资金明细, summary=false, iTradingStatus=12");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create12_UserCancelOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(false);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_007() {
		Logger.start(true, "获取订单资金明细, summary=true, iTradingStatus=13");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create13_SellerCancelOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(true);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_008() {
		Logger.start(true, "获取订单资金明细, summary=true, iTradingStatus=18");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create18_SystemCancelOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(true);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_011() {
		Logger.start(true, "获取订单资金明细, summary=false, iTradingStatus=17");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create17_AcceptOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(false);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_012() {
		Logger.start(true, "获取订单资金明细, summary=null");
		try {
			Logger.comment("准备订单数据");
			OrderCreate o = new OrderCreate();
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int orderId = o.create17_AcceptOrder(userId, sellerId)
					.getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("完成准备订单数据");
			request.setOrderId(orderId);
			request.setSummary(null);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderFundDetail_014() {
		Logger.start(true, "获取订单资金明细, orderId=null");
		try {
			request.setOrderId(null);
			request.setSummary(null);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			Logger.verifyEquals("400", call.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetOrderFundDetail_016() {
		Logger.start(true, "获取订单资金明细, orderId=0");
		try {
			request.setOrderId(0);
			request.setSummary(null);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			Logger.verifyEquals("400", call.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_017() {
		Logger.start(true, "获取订单资金明细, summary=true, iTradingStatus=4");
		try {
			//			Logger.comment("准备订单数据");
			//			OrderCreate o = new OrderCreate();
			//			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			//			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			//			int orderId = o.create4_ReceivedOrder(userId, sellerId).getiOrderId();
			//			Thread.sleep(2000l);
			//			Logger.comment("完成准备订单数据");

			request.setOrderId(112510844);
			request.setSummary(true);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetOrderFundDetail_018() {
		Logger.start(true, "使用宝付支付,获取订单资金明细, summary=true, iTradingStatus=2");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder5();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();

			int thirdTradingId = new OrderCallService().orderPayRequest(userId,
					BigDecimal.ZERO, tradingId, "13");

			PaymentProxyCallService.notifyCall("70",
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(), userId);

			SyncVerifyService.getSyncResult(orderId);
			request.setOrderId(orderId);
			request.setSummary(true);
			String param = FormatUtil.beanToHttpString(request, true, false);
			call.setData(param);
			call.callService();
			// Logger.comment("call send:"
			// + call.getEntityString());
			// Logger.comment("call response:"
			// + call.getReturnData());
			Logger.verifyEquals("200", call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}